/**
 * Created by weng on 2017/8/23.
 */

/**
 * 获取帐号信息
 * @param info
 */
var getUserInfo = function (info) {
    console.log("获取信息");
    $.ajax({
        url:'/controller/getUserInfo',
        type:'post',
        dataType:'json',
        data:{},
        success:function(data){
            if(data.code != 200){
                return ;
            }else{
                var html = "";
                for(var i in data.info){
                    html += '<tr>' +
                        '<th hidden="hidden" id="id'+ data.info[i].id +'">'+  data.info[i].id + '</th>' +
                        '<th  id="account'+ data.info[i].id +'">'+  data.info[i].account + '</th>' +
                        '<th  id="password'+ data.info[i].id +'">'+  data.info[i].password + '</th>' +
                        // '<th><input id="pwd'+ data.info[i].id +'" style="width: 100%" value="'+ data.info[i].password +'"> </input></th>' +
                        "<th><a onclick='updatePassword("+ data.info[i].id +")'>修改密码</a>" +
                        '     <a onclick="delUser('+  data.info[i].id +')">删除账号</a>' +
                        '     <a onclick="changeUserInfo('+  data.info[i].id +')">修改账号权限</a>' +
                        '</th>' +
                        '</tr>'
                }
            }
            console.log("html is:",html);
            var tbody = $('#compneyImageContent tbody');
            var tr = $(html);
            tbody.append(tr);
        }
    });
}

var changeUserInfo = function (id) {
    window.location.href ="/boss/userModify/?id=" + id;
}
/**
 * 修改玩家信息
 * @param id
 */
var updatePassword = function (id) {
    var obj = {};

    layer.prompt({
        formType:1,
        value:'請填寫新密碼',
        title:'修改密碼',
    },function(value,index){
        var obj = {};
        obj.account = $("#account"+ id).html();
        obj.password = value;
        obj.id =id;

        var url="/controller/updatePassword";
        $.ajax({
            type:'POST',
            url:url,
            data:obj,
            async:false,
            success:function(data){
                if (data.code != 200) {
                    layer.msg("修改失败" );
                } else {
                    layer.msg('修改成功', {
                        icon: 1,
                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.reload();
                    });
                }
                layer.close(index);
            }
        });

    });
};


var delUser = function (id) {
    layer.msg('你确定删除么？', {
        time: 0 //不自动关闭
        ,btn: ['确定', '取消']
        ,yes: function(index){
            var obj = {};
            obj.account = $("#account"+ id).html();
            obj.password = $("#pwd"+ id).val();
            obj.id =id;
            $.ajax({
                url:'/controller/delUser',
                type:'post',
                dataType:'json',
                data:obj,
                success:function(data){
                    layer.close(index);
                    if(200 === data.code) {
                        layer.msg('刪除成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }else {
                        layer.msg(data.info );
                    }
                }
            });
        }
    });
};



var addBossUser = function () {
    var obj = {};
    obj.account = $("#taccount").val();
    obj.password = $("#password").val();
    obj.passwordsec = $("#passwordSec").val();

    console.log("obj:",$("#passwordSec").val());
    console.log("obj:",$("#password").val());

    if(obj.password  != obj.passwordsec){
        return layer.msg("密码不一致");
    }
    $.ajax({
        url:'/controller/addBossUser',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data){
            alert(data.info)
            if(200 === data.code) {
                history.go(-1)
            }
        }
    });
};